package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter.ListViewAdapter;
import com.zdsoft.blp.zongbuju.wode.adapter.LiuYanAdapter;
import com.zdsoft.blp.zongbuju.wode.entity.LiuYan;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class WoDeLiuYanActivity extends BaseActivity implements NetCallback,XListView.IXListViewListener{

    private FanHuiView back;
    private XListView xlv;
    private List<LiuYan> list;
    private Integer pageNo = 1;
    private LiuYanAdapter adapter;
    @Override
    protected void init() {
        setContentView(R.layout.wodeliuyan_wode);
        back = find(R.id.back);
        xlv=find(R.id.xlv);
        back.getTv_title().setText("我的留言");
        list = new ArrayList<>();
        getLiuYanData();

        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);

    }

    public void getLiuYanData() {
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId", "");
        if(!"".equals(userId)){
            Net net = new Net();
            net.url = API.PROBLEM_URL;
            net.nameSpace = API.PROBLEM_NAMESPACE;
            net.methodName = API.MYPROPERTYLIST;
            Map<String, Object> map = new HashMap<>();
            map.put("pageNo", pageNo);
            map.put("pageSize", 10);
            map.put("userId", userId);
            map.put("condition", "");
            map.put("curTime",new Date().getTime()+"");
            net.parms = map;
            getServer(net,this);
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag){
            try {
                JSONObject object = new JSONObject(res);
                JSONArray array = object.getJSONArray("rows");
                for(int i =0;i<array.length();i++){
                    JSONObject liuyan = array.getJSONObject(i);
                    List<String> picPathlList = new ArrayList<>();
                    JSONArray picPathlListArray = liuyan.getJSONArray("picPathSet");
                    for(int j = 0;j<picPathlListArray.length();j++){
                        JSONObject picPath = picPathlListArray.getJSONObject(j);
                        picPathlList.add(picPath.getString("picPath"));
                    }
                    list .add(new LiuYan(liuyan.getString("id"),liuyan.getString("content"),liuyan.getString("createTime"),liuyan.getString("headImg"),
                            liuyan.getString("vflength"),
                            liuyan.getString("userName"),
                            picPathlList,liuyan.getString("amrPath"),
                            liuyan.getString("replyNum")));
                }
                if (pageNo == 1) {
                    adapter = new LiuYanAdapter(getApplicationContext(), list);
                    xlv.setAdapter(adapter);
                    xlv.stopRefresh();
                } else {
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                    xlv.stopLoadMore();
                }
                pageNo++;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onRefresh() {
        list = new ArrayList<>();
        pageNo=1;
        getLiuYanData();
    }

    @Override
    public void onLoadMore() {
        getLiuYanData();
    }
}
